package com.xxx.kjds.controller;


import com.xxx.kjds.entity.*;
import com.xxx.kjds.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderGoodsService orderGoodsService;
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private WalletService walletService;
    @Autowired
    private FinanceService financeService;
    @Autowired
    private TraceService traceService;
    @Autowired
    private AddressService addressService;


    /*处于不同状态的订单列表*/
    @RequestMapping(value = "/orderList", method = RequestMethod.POST)
    @ResponseBody
    public List<Order> orderList(HttpServletRequest request, HttpServletResponse response) {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        String state = request.getParameter("state");

        List<Order> orders = orderService.selectByUidAndState(uid, state);
        System.out.println(orders.size());
        for (Order order1 : orders) {
            String state1 = order1.getState();
            switch (state1) {
                case "A":
                    order1.setState("待支付");
                    break;
                case "B":
                    order1.setState("待发货");
                    break;
                case "C":
                    order1.setState("已发货");
                    break;
                case "D":
                    order1.setState("已完成");
                    break;
                default:
                    order1.setState("已取消");
                    break;
            }
        }
        return orders;
    }

    @PostMapping(value = "/orderGoods")
    @ResponseBody
    public List<Goods> orderGoods(HttpServletRequest request) {
        List<Goods> result = new ArrayList<>();

        Integer oid = Integer.valueOf(request.getParameter("oid"));
        List<OrderGoods> list = orderGoodsService.selectByOid(oid);
        for (OrderGoods orderGoods : list) {
            Integer gid = orderGoods.getGoodsId();
            Goods goods = goodsService.selectByPrimaryKey(gid);
            result.add(goods);
        }
        return result;
    }

    @PostMapping(value = "/orderPay")
    @ResponseBody
    public String orderPay(HttpServletRequest request) {
        /*需完成插入地址，钱包扣钱，修改订单状态和地址ID,流水记录*/

        //订单Id,借卖方Id,品牌商Id,订单金额
        int orderId = Integer.parseInt(request.getParameter("orderId"));
        int bvo_id = Integer.parseInt(request.getParameter("bid"));
        int mvo_id = Integer.parseInt(request.getParameter("mid"));
        double totalMoney = Double.parseDouble(request.getParameter("totalMoney"));

        //判断金额满足条件后执行后续操作
        Wallets bvoWallets = walletService.selectByUid(bvo_id);
        Integer bWid = bvoWallets.getId();
        Wallets mvoWallets = walletService.selectByUid(mvo_id);
        Integer mWid = mvoWallets.getId();

        if (bvoWallets.getMoney() >= totalMoney) {
            //修改金额
            bvoWallets.setMoney(bvoWallets.getMoney() - totalMoney);
            walletService.updateByPrimaryKey(bvoWallets);
            mvoWallets.setMoney(mvoWallets.getMoney() + totalMoney);
            walletService.updateByPrimaryKey(mvoWallets);

            //插入流水记录
            Date date = new Date();

            //借卖方提现记录
            FinanceCheck bvoFinanceCheck = new FinanceCheck();
            bvoFinanceCheck.setType("提现");
            bvoFinanceCheck.setMoney(totalMoney);
            bvoFinanceCheck.setApplyTime(date);
            bvoFinanceCheck.setState("已审核(通过)");
            financeService.insert(bvoFinanceCheck);
            List<FinanceCheck> FinanceChecksOne = financeService.getList();
            int bvoFcId = FinanceChecksOne.get(FinanceChecksOne.size() - 1).getId();

            TraceKey bvoTrace = new TraceKey();
            bvoTrace.setWalletsId(bWid);
            bvoTrace.setFcId(bvoFcId);
            traceService.insert(bvoTrace);

            //品牌商充值记录
            FinanceCheck mvoFinanceCheck = new FinanceCheck();
            mvoFinanceCheck.setType("充值");
            mvoFinanceCheck.setMoney(totalMoney);
            mvoFinanceCheck.setApplyTime(date);
            mvoFinanceCheck.setState("已审核(通过)");

            financeService.insert(mvoFinanceCheck);
            List<FinanceCheck> FinanceChecksTwo = financeService.getList();
            int mvoFcId = FinanceChecksTwo.get(FinanceChecksTwo.size() - 1).getId();
            TraceKey mvoTrace = new TraceKey();
            mvoTrace.setWalletsId(mWid);
            mvoTrace.setFcId(mvoFcId);
            traceService.insert(mvoTrace);


            //插入地址
            Address address = new Address();
            address.setId(orderId);
            address.setCountry(request.getParameter("country"));
            address.setProvince(request.getParameter("province"));
            address.setCity(request.getParameter("city"));
            address.setDetails(request.getParameter("details"));
            addressService.insert(address);

            //修改订单的状态和地址ID
            Order order = orderService.selectByPrimaryKey(orderId);
            order.setState("B");
            order.setId(orderId);
            order.setAddressId(orderId);
            orderService.updateByPrimaryKey(order);

            return "支付成功";
        }
        return "支付失败";
    }


    @PostMapping("/selectByPrimaryKey")
    @ResponseBody
    public Order selectByPrimaryKey(Integer id) {
        return orderService.selectByPrimaryKey(id);
    }

    @PostMapping("/selectToBePaidFromOrder")
    @ResponseBody
    public List<Order> selectToBePaidFromOrder(Integer mvoId) {
        System.out.println(orderService.selectToBePaidFromOrder(mvoId));

        return orderService.selectToBePaidFromOrder(mvoId);
    }

    @PostMapping("/selectToBeDeliveredFromOrder")
    @ResponseBody
    public List<Order> selectToBeDeliveredFromOrder(Integer mvoId) {

        //System.out.println(orderService.selectToBeDeliveredFromOrder(mvoId) + "asdasdfsadfsfsdgsdfgsdfgsdfgdsfgdsfgsdfgsfsd");
        return orderService.selectToBeDeliveredFromOrder(mvoId);
    }

    @PostMapping("/selectDeliveredFromOrder")
    @ResponseBody
    public List<Order> selectDeliveredFromOrder(Integer mvoId) {

        return orderService.selectDeliveredFromOrder(mvoId);
    }

    @PostMapping("/selectFinishedFromOrder")
    @ResponseBody
    public List<Order> selectFinishedFromOrder(Integer mvoId) {
        return orderService.selectFinishedFromOrder(mvoId);
    }

    @PostMapping("/selectCanceledFromOrder")
    @ResponseBody
    public List<Order> selectCanceledFromOrder(Integer mvoId) {
        return orderService.selectCanceledFromOrder(mvoId);
    }

    @PostMapping("/updateOrderState")
    @ResponseBody
    public String updateOrderState(Integer id, String state) {
        int re = orderService.updateOrderState(id, state);

        if (state.equals("C")) {
            if (re == 1) return "已发货";
            return "发货失败";
        } else {
            if (re == 1) return "已取消";
            return "取消失败";
        }

    }
}

